package smartbooks.ui.items;

import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;
import smartbooks.exceptions.MyException;
import smartbooks.restrictDocuments.JTextFieldMaxLimitDocument;
import smartbooks.ui.SmartBooksUserSettings;
import smartbooks.ui.listeners.CommonMouseListener;
import smartbooks.ui.mainframe.MainFrame;
import smartbooks.ui.manager.ItemManager;
import static smartbooks.ui.util.CommonConstants.*;
import smartbooks.ui.util.CommonFunctions;
import smartbooksdb.exception.MandatoryFieldException;
import smartbooksdb.hibernatebean.Item;

/**
 *
 * @author Ankit
 */
public class ItemsPanel extends javax.swing.JPanel {
    private Item editItem = null;
    Item item = new Item();
    
    /**
     * Creates new form ItemsPanel
     * @param editItem
     */
    public ItemsPanel(Item editItem) {
        // We set this in item object initially so that we can set the default here first then it can be modified
        item.setManageInventoryStocks(false);

        if(null!=editItem) {
            this.editItem = editItem;
            this.item = editItem;
        }

        initComponents();

        customInitComponents();
    }
    
    private void customInitComponents() {
        addKeyListenersToComponents(keyListener);
        
        if(null==editItem) {
            itemPanel.setBorder(new TitledBorder(itemPanel.getBorder(), ITEM_PANEL_CREATE_TEXT, 2, 0, new Font("Default", 0, 14)));
            createOrSaveItemButton.setText(CREATE_BUTTON_TEXT);
        } else {
            itemPanel.setBorder(new TitledBorder(itemPanel.getBorder(), ITEM_PANEL_EDIT_TEXT, 2, 0, new Font("Default", 0, 14)));
            createOrSaveItemButton.setText(SAVE_BUTTON_TEXT);
            
            costPriceFormattedTextField.setText(String.valueOf(item.getCostPrice()));
            sellingPriceFormattedTextField.setText(String.valueOf(item.getSellingPrice()));
            // Set Vendor Name
            // Set Tax Name
            currentQuantityFormattedTextField.setText(String.valueOf(item.getCurrentQuantity()));
            idealQuantityFormattedTextField.setText(String.valueOf(item.getIdealQuantity()));
        }
        
        populateVendor();
        populateItemTax();
    }
    
    private void addKeyListenersToComponents(KeyListener keyListener) {
        itemNameTextField.addKeyListener(keyListener);
        itemCodeTextField.addKeyListener(keyListener);
        costPriceFormattedTextField.addKeyListener(keyListener);
        sellingPriceFormattedTextField.addKeyListener(keyListener);
        vendorNameComboBox.addKeyListener(keyListener);
        itemTaxComboBox.addKeyListener(keyListener);
        manageInventoryCheckBox.addKeyListener(keyListener);
        currentQuantityFormattedTextField.addKeyListener(keyListener);
        idealQuantityFormattedTextField.addKeyListener(keyListener);
    }
    
    private void populateVendor() {
        ArrayList vendorNames = new ArrayList();
        vendorNames.add(DEFAULT_VENDOR);
        
        vendorNameComboBox.setModel(new DefaultComboBoxModel(vendorNames.toArray()));
    }
    
    private void populateItemTax() {
        ArrayList itemTaxNames = new ArrayList();
        itemTaxNames.add(DEFAULT_ITEM_TAX);
        
        itemTaxComboBox.setModel(new DefaultComboBoxModel(itemTaxNames.toArray()));
    }
    
    /**
     * This method is used to get Item
     * @return item
     */
    public Item getItem() {
        return this.item;
    }
    
    /**
     * This method is used to set Item
     * @param item
     */
    public void setCustomer(Item item) {
        this.item = item;
    }
    
    private void assignAdditionalItemValues(String itemModificationType) throws MyException {
        if(null!=costPriceFormattedTextField.getText() && costPriceFormattedTextField.getText().length()>0) {
            item.setCostPrice(Double.valueOf(costPriceFormattedTextField.getText()));
        }
        if(null!=sellingPriceFormattedTextField.getText() && sellingPriceFormattedTextField.getText().length()>0) {
            item.setSellingPrice(Double.valueOf(sellingPriceFormattedTextField.getText()));
        } else {
            throw new MyException(SELLING_PRICE, "Selling Price cannot be blank");
        }
        
        // setVendorIds
        // setItemTaxIds
        
        if(null!=currentQuantityFormattedTextField.getText() && currentQuantityFormattedTextField.getText().length()>0) {
            item.setCurrentQuantity(Long.valueOf(currentQuantityFormattedTextField.getText()));
        }
        if(null!=idealQuantityFormattedTextField.getText() && idealQuantityFormattedTextField.getText().length()>0) {
            item.setIdealQuantity(Long.valueOf(idealQuantityFormattedTextField.getText()));
        }
        
        if(itemModificationType.equals(CREATE_BUTTON_TEXT)) {
            item.setCreatedBy(SmartBooksUserSettings.USERNAME);
        }
        item.setUpdatedBy(SmartBooksUserSettings.USERNAME);
    }
    
    private void setModifiedItemInParent() {
        Container c = this;
        
        while(c.getParent()!=null) {
            c = c.getParent();
            
            if(c instanceof ItemsDialog) {
                ((ItemsDialog)c).setModifiedItem(item);
            }
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        itemPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        itemNameTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        itemCodeTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        itemDescriptionScrollPane = new javax.swing.JScrollPane();
        itemDescriptionTextArea = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        costPriceFormattedTextField = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        sellingPriceFormattedTextField = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        vendorNameComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        itemTaxComboBox = new javax.swing.JComboBox();
        manageInventoryCheckBox = new javax.swing.JCheckBox();
        inventoryPanel = new javax.swing.JPanel();
        currentQuantityLabel = new javax.swing.JLabel();
        currentQuantityFormattedTextField = new javax.swing.JFormattedTextField();
        idealQuantityLabel = new javax.swing.JLabel();
        idealQuantityFormattedTextField = new javax.swing.JFormattedTextField();
        clearItemButton = new javax.swing.JButton();
        createOrSaveItemButton = new javax.swing.JButton();

        itemPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " ", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("<html><font color=red>* </font>Item Name:</html>");

        itemNameTextField.setDocument(new JTextFieldMaxLimitDocument(64));
        itemNameTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${item.itemName}"), itemNameTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "itemNameBinding");
        bindingGroup.addBinding(binding);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Item Code:");

        itemCodeTextField.setDocument(new JTextFieldMaxLimitDocument(64));
        itemCodeTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${item.itemCode}"), itemCodeTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "itemCodeBinding");
        bindingGroup.addBinding(binding);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Item Description:");

        itemDescriptionScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        itemDescriptionTextArea.setColumns(20);
        itemDescriptionTextArea.setDocument(new JTextFieldMaxLimitDocument(256));
        itemDescriptionTextArea.setLineWrap(true);
        itemDescriptionTextArea.setRows(4);
        itemDescriptionTextArea.setWrapStyleWord(true);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${item.itemDescription}"), itemDescriptionTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"), "itemDescriptionBinding");
        bindingGroup.addBinding(binding);

        itemDescriptionTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                itemDescriptionTextAreaKeyPressed(evt);
            }
        });
        itemDescriptionScrollPane.setViewportView(itemDescriptionTextArea);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Cost Price:");

        costPriceFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.##"))));
        costPriceFormattedTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        costPriceFormattedTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                costPriceFormattedTextFieldKeyTyped(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("<html><font color=red>* </font>Selling Price:</html>");

        sellingPriceFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.##"))));
        sellingPriceFormattedTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        sellingPriceFormattedTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                sellingPriceFormattedTextFieldKeyTyped(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("Vendor Name:");

        vendorNameComboBox.setEditable(true);
        vendorNameComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Item Tax:");

        itemTaxComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        manageInventoryCheckBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        manageInventoryCheckBox.setText("Manage the Inventory stocks");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${item.manageInventoryStocks}"), manageInventoryCheckBox, org.jdesktop.beansbinding.BeanProperty.create("selected"), "manageInventoryStocksBinding");
        bindingGroup.addBinding(binding);

        manageInventoryCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                manageInventoryCheckBoxStateChanged(evt);
            }
        });

        inventoryPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Inventory", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        inventoryPanel.setEnabled(false);

        currentQuantityLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        currentQuantityLabel.setText("Current Quantity:");
        currentQuantityLabel.setEnabled(false);

        currentQuantityFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.##"))));
        currentQuantityFormattedTextField.setEnabled(false);
        currentQuantityFormattedTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        currentQuantityFormattedTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                currentQuantityFormattedTextFieldKeyTyped(evt);
            }
        });

        idealQuantityLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        idealQuantityLabel.setText("Ideal Quantity:");
        idealQuantityLabel.setEnabled(false);

        idealQuantityFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.##"))));
        idealQuantityFormattedTextField.setEnabled(false);
        idealQuantityFormattedTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        idealQuantityFormattedTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                idealQuantityFormattedTextFieldKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout inventoryPanelLayout = new javax.swing.GroupLayout(inventoryPanel);
        inventoryPanel.setLayout(inventoryPanelLayout);
        inventoryPanelLayout.setHorizontalGroup(
            inventoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(inventoryPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(inventoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(currentQuantityLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(idealQuantityLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(inventoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(currentQuantityFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(idealQuantityFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        inventoryPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {currentQuantityFormattedTextField, idealQuantityFormattedTextField});

        inventoryPanelLayout.setVerticalGroup(
            inventoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(inventoryPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(inventoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(currentQuantityLabel)
                    .addComponent(currentQuantityFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(inventoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(idealQuantityLabel)
                    .addComponent(idealQuantityFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        clearItemButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        clearItemButton.setText("Clear");
        clearItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearItemButtonActionPerformed(evt);
            }
        });

        createOrSaveItemButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        createOrSaveItemButton.setText("Create");
        createOrSaveItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createOrSaveItemButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout itemPanelLayout = new javax.swing.GroupLayout(itemPanel);
        itemPanel.setLayout(itemPanelLayout);
        itemPanelLayout.setHorizontalGroup(
            itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, itemPanelLayout.createSequentialGroup()
                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, itemPanelLayout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, itemPanelLayout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(costPriceFormattedTextField))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, itemPanelLayout.createSequentialGroup()
                                .addComponent(clearItemButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(createOrSaveItemButton))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, itemPanelLayout.createSequentialGroup()
                        .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(itemPanelLayout.createSequentialGroup()
                                .addGap(11, 11, 11)
                                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(itemNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(itemDescriptionScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                                        .addComponent(itemCodeTextField, javax.swing.GroupLayout.Alignment.TRAILING))))
                            .addGroup(itemPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(manageInventoryCheckBox)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(itemPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inventoryPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, itemPanelLayout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(vendorNameComboBox, 0, 217, Short.MAX_VALUE)
                                    .addComponent(sellingPriceFormattedTextField)
                                    .addComponent(itemTaxComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );

        itemPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {clearItemButton, createOrSaveItemButton});

        itemPanelLayout.setVerticalGroup(
            itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(itemNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel5)
                    .addComponent(itemCodeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(itemDescriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel3)
                    .addComponent(costPriceFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sellingPriceFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(vendorNameComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(itemTaxComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(manageInventoryCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(inventoryPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(itemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createOrSaveItemButton)
                    .addComponent(clearItemButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(itemPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(itemPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void manageInventoryCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_manageInventoryCheckBoxStateChanged
        MainFrame.setEnabledAllComponents(inventoryPanel, manageInventoryCheckBox.isSelected());
    }//GEN-LAST:event_manageInventoryCheckBoxStateChanged

    private void clearItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearItemButtonActionPerformed
        itemNameTextField.setText("");
        itemCodeTextField.setText("");
        itemDescriptionTextArea.setText("");
        costPriceFormattedTextField.setText("");
        sellingPriceFormattedTextField.setText("");
        vendorNameComboBox.setSelectedIndex(0);
        itemTaxComboBox.setSelectedIndex(0);
        manageInventoryCheckBox.setSelected(false);
    }//GEN-LAST:event_clearItemButtonActionPerformed

    private void costPriceFormattedTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_costPriceFormattedTextFieldKeyTyped
        char c = evt.getKeyChar();  
        if (c=='-') {
            evt.consume();
        }
    }//GEN-LAST:event_costPriceFormattedTextFieldKeyTyped

    private void sellingPriceFormattedTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sellingPriceFormattedTextFieldKeyTyped
        char c = evt.getKeyChar();  
        if (c=='-') {
            evt.consume();
        }
    }//GEN-LAST:event_sellingPriceFormattedTextFieldKeyTyped

    private void currentQuantityFormattedTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_currentQuantityFormattedTextFieldKeyTyped
        char c = evt.getKeyChar();  
        if (c=='-') {
            evt.consume();
        }
    }//GEN-LAST:event_currentQuantityFormattedTextFieldKeyTyped

    private void idealQuantityFormattedTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_idealQuantityFormattedTextFieldKeyTyped
        char c = evt.getKeyChar();  
        if (c=='-') {
            evt.consume();
        }
    }//GEN-LAST:event_idealQuantityFormattedTextFieldKeyTyped

    private void itemDescriptionTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_itemDescriptionTextAreaKeyPressed
        CommonFunctions.textboxTABPolicy(evt);
    }//GEN-LAST:event_itemDescriptionTextAreaKeyPressed

    private void createOrSaveItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createOrSaveItemButtonActionPerformed
        CommonMouseListener.hideException = false;
        
        try {
            assignAdditionalItemValues(createOrSaveItemButton.getText());
            
            boolean itemModified = false;
            switch (createOrSaveItemButton.getText()) {
                case CREATE_BUTTON_TEXT:
                    itemModified = ItemManager.createItem(item);
                    break;
                case SAVE_BUTTON_TEXT:
                    itemModified = ItemManager.updateItem(item, editItem);
                    break;
            }
            
            if(itemModified) {
                Toolkit.getDefaultToolkit().beep();
                
                switch (createOrSaveItemButton.getText()) {
                    case CREATE_BUTTON_TEXT:
                        JOptionPane.showMessageDialog(this,"Item is created successfully !","SmartBooks - Success",JOptionPane.INFORMATION_MESSAGE);
                        break;
                    case SAVE_BUTTON_TEXT:
                        JOptionPane.showMessageDialog(this,"Item is updated successfully !","SmartBooks - Success",JOptionPane.INFORMATION_MESSAGE);
                        break;
                }
                
                setModifiedItemInParent();
                CommonFunctions.dispose(this);
            } else {
                Toolkit.getDefaultToolkit().beep();
                JOptionPane.showMessageDialog(this,"There was some error in creating Item.\nKindly contact system administrator.","SmartBooks - Error",JOptionPane.ERROR_MESSAGE);
            }
        } catch (MyException me) {
            if(me.getComponentText().equalsIgnoreCase(ITEM_NAME)) {
                me.showBalloonTip(itemNameTextField);
                itemNameTextField.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(SELLING_PRICE)) {
                me.showBalloonTip(sellingPriceFormattedTextField);
                sellingPriceFormattedTextField.requestFocus();
            }
        } catch (MandatoryFieldException ex) {
            Logger.getLogger(ItemsPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_createOrSaveItemButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clearItemButton;
    private javax.swing.JFormattedTextField costPriceFormattedTextField;
    private javax.swing.JButton createOrSaveItemButton;
    private javax.swing.JFormattedTextField currentQuantityFormattedTextField;
    private javax.swing.JLabel currentQuantityLabel;
    private javax.swing.JFormattedTextField idealQuantityFormattedTextField;
    private javax.swing.JLabel idealQuantityLabel;
    private javax.swing.JPanel inventoryPanel;
    private javax.swing.JTextField itemCodeTextField;
    private javax.swing.JScrollPane itemDescriptionScrollPane;
    private javax.swing.JTextArea itemDescriptionTextArea;
    private javax.swing.JTextField itemNameTextField;
    private javax.swing.JPanel itemPanel;
    private javax.swing.JComboBox itemTaxComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JCheckBox manageInventoryCheckBox;
    private javax.swing.JFormattedTextField sellingPriceFormattedTextField;
    private javax.swing.JComboBox vendorNameComboBox;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    
    KeyListener keyListener = new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {}

        @Override
        public void keyPressed(KeyEvent e) {}

        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER) { 
                createOrSaveItemButtonActionPerformed(null);
            }
        }
    };
}
